package controller.impl;

import java.util.List;

import model.bo.Topics;
import model.dao.ITopicsDAO;
import controller.dao.ITopicsService;

public class TopicsService implements ITopicsService {

	private ITopicsDAO ITopicsDAO;

	public ITopicsDAO getITopicsDAO() {
		return ITopicsDAO;
	}

	public void setITopicsDAO(ITopicsDAO topicsDAO) {
		ITopicsDAO = topicsDAO;
	}

	public boolean createOrUpdate(Topics topics) {
		// 创建或者更新
		try{
			ITopicsDAO.save(topics);
			return true;
		}catch(Exception ex){
			ex.printStackTrace();
		}
		return false;
	}
	
	public boolean delete(String articleArr) {
		// 删除
		try{
			String sql = "delete from topics where id in ("+articleArr+")";
			ITopicsDAO.execusql(sql);
			return true;
		}catch(Exception ex){
			ex.printStackTrace();
		}
		return false;
	}

	public List<Topics> getList(String pageNumber,Integer offset) {
		// 查询出列表
		String hql = "from Topics order by updatetime desc";
		Integer length = ITopicsDAO.getAllRowCount(hql);
		Integer s = new Integer(pageNumber);
		List queryForPage = ITopicsDAO.queryForPage(hql, s, offset);
		List<Topics> list = queryForPage;
		return list;
	}
	
	public String getJsonList(String pageNumber,Integer offset){
		String hql = "from Topics order by id desc";
		Integer length = ITopicsDAO.getAllRowCount(hql);
		Integer s = new Integer(pageNumber);
		List queryForPage = ITopicsDAO.queryForPage(hql, s, offset);
		List<Topics> list = queryForPage;
		String result = util.JsonUtil.listToJson(length, list);
		return result;
	}
	
	public Topics findById(Integer id){
		return ITopicsDAO.findById(id);
	}

	public List<Topics> findAll() {
		// TODO Auto-generated method stub
		return ITopicsDAO.findAll();
	}
	
}
